package com.sky.es.web;

import com.sky.es.service.IElasticsearchService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class ElasticsearchController {

    @Resource
    private IElasticsearchService elasticsearchService;

    @GetMapping("/save")
    public String saveIndex(){
        elasticsearchService.insert();
        return "索引创建完成";
    }

    @GetMapping("/batchInsert")
    public String batchInsert(){
        elasticsearchService.batchInsert();
        return "批量添加索引数据完成";
    }

    @GetMapping("/saveNew")
    public String saveNewIndex(){
        elasticsearchService.insertNew();
        return "索引创建完成";
    }

    @GetMapping("/findById")
    @ResponseBody
    public Object findById(@RequestParam String id,
                           @RequestParam String indexName){

        //return elasticsearchService.findById(id);
        return elasticsearchService.findById(indexName, id);
    }

    @GetMapping("/findPage")
    @ResponseBody
    public Object findPage(@RequestParam String key,
                           @RequestParam Integer start,
                           @RequestParam Integer end,
                           @RequestParam Integer page,
                           @RequestParam Integer size,
                           @RequestParam String title){
        return elasticsearchService.findByBlogIdPage(title, key, start, end, page, size);
    }

    @GetMapping("/deleteIndexByQuery")
    @ResponseBody
    public Object deleteIndexByQuery(@RequestParam String indexName,
                                     @RequestParam String fieldName,
                                     @RequestParam String value){
        return elasticsearchService.deleteIndexByQuery(indexName, fieldName, value);
    }

    @GetMapping("/updateIndexByQuery")
    @ResponseBody
    public Object updateIndexByQuery(@RequestParam String indexName,
                                     @RequestParam String fieldName,
                                     @RequestParam String value){
        return elasticsearchService.updateIndexByQuery(indexName, fieldName, value);
    }

    @GetMapping("/findByKey")
    @ResponseBody
    public Object findByKey(@RequestParam String title,
                            @RequestParam Long blogId,
                            @RequestParam String summary) {
        return elasticsearchService.findByKey(title, blogId, summary);
    }

    @GetMapping("/findPageResult")
    @ResponseBody
    public Object findPageResult(@RequestParam Integer start,
                                 @RequestParam Integer end,
                                 @RequestParam Integer page,
                                 @RequestParam Integer size){
        return elasticsearchService.findPageResult(start, end, page, size);
    }

    @GetMapping("/deleteIndex")
    public Object deleteIndex(){
        return elasticsearchService.deleteIndex();
    }

    @GetMapping("/updateIndex")
    public Object updateIndex(){
        return elasticsearchService.updateIndex();
    }

    @GetMapping("/getCount")
    public Object getCount(@RequestParam String indexName){
        return elasticsearchService.getCount(indexName);
    }

    @GetMapping("/findAll")
    public Object findAll(@RequestParam String indexName){
        return elasticsearchService.findAll(indexName);
    }

    @GetMapping("/create")
    public String createIndexAndMapping(){
        elasticsearchService.createIndexAndMapping();
        return "索引创建完成";
    }

    @GetMapping("/batchCreateIndex")
    public String batchCreateIndex(){
        elasticsearchService.batchCreateIndex();
        return "批量索引创建完成";
    }
}
